package cn.tedu.property.management.controller;

import cn.tedu.property.management.pojo.dto.CommunityAddNewDTO;
import cn.tedu.property.management.pojo.dto.CommunityUpdateDTO;
import cn.tedu.property.management.pojo.vo.CommunityStandardVO;
import cn.tedu.property.management.service.ICommunityService;
import cn.tedu.property.management.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/community")
@Api(tags = "小区模块")
public class CommunityController {
    @Autowired
    private ICommunityService communityService;
    @ApiOperation("添加小区")
    @ApiOperationSupport(order = 10)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(CommunityAddNewDTO communityAddNewDTO){
        communityService.insert(communityAddNewDTO);
        return JsonResult.ok();
    }
    @ApiOperation("根据id删除小区")
    @ApiOperationSupport(order = 100)
    @ApiImplicitParam(name = "id", value = "小区ID", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id){
        communityService.deleteById(id);
        return JsonResult.ok();
    }
    @ApiOperation("根据id查询小区")
    @ApiOperationSupport(order = 200)
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<CommunityStandardVO> getCommunityById(@PathVariable Long id){
        CommunityStandardVO community = communityService.getCommunityById(id);
        return JsonResult.ok(community);
    }
    @ApiOperation("查询小区")
    @ApiOperationSupport(order = 300)
    @GetMapping("/select")
    public JsonResult<List<CommunityStandardVO>> getCommunity(){
        List<CommunityStandardVO> community = communityService.getCommunity();
        return JsonResult.ok(community);
    }
    @ApiOperation("修改小区")
    @ApiOperationSupport(order = 400)
    @PostMapping("/update/{id}")
    public JsonResult updateCommunity(
            @PathVariable Long id, CommunityUpdateDTO communityUpdateDTO){
        communityService.updateById(id,communityUpdateDTO);
        return JsonResult.ok("更新成功！");
    }
}
